#!/usr/bin/env python3
# coding: utf-8
"""
2025-11-05 检查项目方案浏览界面
"""

from PySide6.QtCore import Slot, QSize
from PySide6.QtGui import QAction, QKeySequence,QIntValidator, QTextCursor   
from PySide6.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QWidget, QMessageBox, QLineEdit, QTableWidget, QTableWidgetItem, QHeaderView, QAbstractItemView, QLabel,QCheckBox,QTextEdit,QGroupBox, QComboBox
from PySide6.QtCore import Qt, QModelIndex,QMetaObject,Q_ARG
from PySide6.QtGui import QGuiApplication, QCloseEvent
## from PySide6.QtCore.Qt import 


##from PySide6.QtCore import WindowState

## from top.wardyan.core.utils import winformutils
from top.wardyan.core.action.baseaction import WMainWindow,WQdialog
from top.wardyan.core.vo.vos import *
from top.wardyan.core.widges.tables import WTableView

##from warningmain import *
from top.wardyan.core.wdb.core.DBPool import POOL

from top.wardyan.core.vo.vos import *
from top.wardyan.core.wdb.core.db import *
from config import Config
from top.wardyan.core.sqlite import sqliteconfigs

import threading
import time
import re
import sys

from apscheduler.schedulers.background import BackgroundScheduler
from top.wardyan.core.utils.common import *
from top.wardyan.projects.checktool_manager.main.checktool_manager_yt_checkplan_edit import *

import uuid

from top.wardyan.projects.checktool_manager.main.checktool_manager_const import *


SQL_LISTALL = "SELECT * FROM yt_checkplan WHERE (sID LIKE %s OR xName LIKE %s) ORDER BY sID"



        

class ChecktoolCheckplanShell (WQdialog) :
        
    def __init__(self) -> None:
        ##global cn
        super().__init__("ChecktoolCheckplanShell")
        self.table : WTableView = None  # type: ignore
        self.main()
        self.refreshTable()


    def button_click_search(self) :
        self.refreshTable()


    def button_click_add(self) :
        win = ChecktoolCheckplanEditShell(0)
        rs : int = win.exec()
        self.refreshTable()


    
    def handle_double_click(self, index: QModelIndex) -> None:
        """
        处理单元格的双击事件。
        :param index: 被双击的单元格的模型索引。
        """
        row = index.row()
        column = index.column()
        ##print(f"Double click detected at Row: {row}, Column: {column}")
        data : Record = self.table.get_row_data(row)
        if data is not None :
            field = data.get("sID")
            ## win = TableColEditor("tablemanagercoledit", self.tablename, field)
            win = ChecktoolCheckplanEditShell(field)
            # win.exec()
            rs : int = win.exec()
            # if rs is not None and rs > 0 :
            self.refreshTable()


    def main(self) :

        HEIGHT = 40
        layout : QVBoxLayout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignVCenter)
        self.setLayout(layout)
        
        

        ## 按钮: 搜索 新增
        topwidget : QWidget = QWidget()
        ##topwidget.setFixedHeight(100)
        layout.addWidget(topwidget)
        topwidget.setFixedHeight(50)
        
        # 设置背景颜色
        #topwidget.setStyleSheet("background-color: #ffff00;padding:0;margin:0;")  # 你可以将 #f0f0f0 替换为你想要的颜色代码

        toplayout : QHBoxLayout = QHBoxLayout()
        toplayout.setAlignment(Qt.AlignmentFlag.AlignRight)
        ##self.setLayout(layout)
        ##layout.addWidget(toplayout)
        topwidget.setLayout(toplayout)
        

        self.txtKey : QLineEdit = QLineEdit()
        self.txtKey.setFixedHeight(HEIGHT)
        self.txtKey.setFixedWidth(200)
        self.txtKey.setText("")
        toplayout.addWidget(self.txtKey)
        ##self.txtKey.textChanged.connect(self.text_max_idle_change)
        ## txtDesc只允许输入数字
        ##self.txtKey.setValidator(QIntValidator())
        # 连接回车键事件到 button_click_search 方法
        self.txtKey.returnPressed.connect(self.button_click_search)



        ### 
        

        btnSearch : QPushButton = QPushButton("搜索")
        btnSearch.setFixedHeight(HEIGHT)
        btnSearch.setFixedWidth(150)
        btnSearch.clicked.connect(self.button_click_search)
        btnSearch.setDefault(True)
        toplayout.addWidget(btnSearch)



        btnAdd : QPushButton = QPushButton("新增")
        btnAdd.setFixedHeight(HEIGHT)
        btnAdd.setFixedWidth(150)
        btnAdd.clicked.connect(self.button_click_add)
        toplayout.addWidget(btnAdd)

        ###################

        ctrlw : QWidget = QWidget()
        ctrllayout : QHBoxLayout = QHBoxLayout()
        ctrlw.setLayout(ctrllayout)
        layout.addWidget(ctrlw)


        ctrllayout.setAlignment(Qt.AlignmentFlag.AlignRight)
        

        ctrlt : QWidget = QWidget()
        tbllayout : QHBoxLayout = QHBoxLayout()
        ctrlt.setLayout(tbllayout)
        layout.addWidget(ctrlt)

        self.table = WTableView(self, "checktool_manager_checkplan", "maintable", dbclick = self.handle_double_click)
        tbllayout.addWidget(self.table)

    @Slot()
    def refreshTable(self) :

        data : list[Record] = []
        # 查看正在执行的语句
        key : str = self.txtKey.text()
        if key is None :
            key = ""
        if key == " " :
            key = ""
        if key == "" :
            key = "%%"
        else:
            key = "%" + key + "%"
        ## 暂时不限制日期范围等条件
        rs = query(sql=SQL_LISTALL, params= (key, key),config=MYCONFIG)
        if rs is None :
            rs = []
        data = listtorecords(rs)
        ## print(rs)
        self.data = data
        self.table.render(data)